//
//  KeyboardManager.m
//  ClienteHabitueIOS
//
//  Created by Mauro Carreño on 10/24/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "KeyboardManager.h"
#import "SharedMemory.h"
#import "ControlVariables.h"

@implementation KeyboardManager

@synthesize scrollView;
@synthesize backgroundButton;
@synthesize elevation;

- (void)dealloc {
    // unregister for keyboard notifications while not visible
    [self unregisterKeyboard];
    self.scrollView = nil;
    self.backgroundButton = nil;
    [super dealloc];    
}


- (id)initWithDelegate:(UIButton *)button scrollView:(UIScrollView*)scroller elevation:(int) _elevation;
{
    self = [super init];
    if (self) {
        self.scrollView = scroller;
        self.backgroundButton = button;
        self.elevation = _elevation;
        [self registerKeyboard];
    }
    return self;
}

- (void)registerKeyboard
{
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    keyboardIsShown = NO;
}

- (void)unregisterKeyboard
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIKeyboardWillShowNotification 
                                                  object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIKeyboardWillHideNotification 
                                                  object:nil];
}

#pragma mark - Keyboard hiding


// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWillShow:(NSNotification*)aNotification
{
    ((UIButton*)self.backgroundButton).enabled = YES;
    // This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScrollView if the keyboard is already shown.  This can happen if the user, after fixing editing a UITextField, scrolls the resized UIScrollView to another UITextField and attempts to edit the next UITextField.  If we were to resize the UIScrollView again, it would be disastrous.  NOTE: The keyboard notification will fire even when the keyboard is already shown.
    if (keyboardIsShown) {
        return;
    }
    
    
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    ((UIScrollView*)self.scrollView).contentInset = contentInsets;
    ((UIScrollView*)self.scrollView).scrollIndicatorInsets = contentInsets;
    
    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    /*CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
    }*/
    CGRect viewFrame = ((UIScrollView*)self.scrollView).frame;
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
    viewFrame.origin.y -= (kbSize.height - self.elevation);
    [((UIScrollView*)self.scrollView) scrollRectToVisible:viewFrame animated:YES];
    keyboardIsShown = YES;
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillHide:(NSNotification*)aNotification
{
    ((UIButton*)self.backgroundButton).enabled = NO;
    CGRect viewFrame = CGRectMake(0, 0, 0, 0);
    [((UIScrollView*)self.scrollView) scrollRectToVisible:viewFrame animated:YES];
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    ((UIScrollView*)self.scrollView).contentInset = contentInsets;
    ((UIScrollView*)self.scrollView).scrollIndicatorInsets = contentInsets;
    keyboardIsShown = NO;
}

/*
- (void)keyboardWillHide:(NSNotification *)n
{
    ((UIButton*)self.backgroundButton).enabled = NO;
    NSDictionary* userInfo = [n userInfo];
    
    // get the size of the keyboard
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    
    // resize the scrollview
    CGRect viewFrame = ((UIScrollView*)self.scrollView).frame;
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
    viewFrame.origin.y += (keyboardSize.height - self.elevation);
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    // The kKeyboardAnimationDuration I am using is 0.3
    [UIView setAnimationDuration:kKeyboardAnimationDuration];
    [self.scrollView setFrame:viewFrame];
    [UIView commitAnimations];
    
    keyboardIsShown = NO;
}

- (void)keyboardWillShow:(NSNotification *)n
{
    ((UIButton*)self.backgroundButton).enabled = YES;
    // This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScrollView if the keyboard is already shown.  This can happen if the user, after fixing editing a UITextField, scrolls the resized UIScrollView to another UITextField and attempts to edit the next UITextField.  If we were to resize the UIScrollView again, it would be disastrous.  NOTE: The keyboard notification will fire even when the keyboard is already shown.
    if (keyboardIsShown) {
        return;
    }
    
    NSDictionary* userInfo = [n userInfo];
    
    // get the size of the keyboard
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    // resize the noteView
    CGRect viewFrame = ((UIScrollView*)self.scrollView).frame;
    
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
    viewFrame.origin.y -= (keyboardSize.height - self.elevation);
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    // The kKeyboardAnimationDuration I am using is 0.3
    [UIView setAnimationDuration:kKeyboardAnimationDuration];
    [self.scrollView setFrame:viewFrame];
    [UIView commitAnimations];
    
    keyboardIsShown = YES;
}*/



@end
